﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Helper;
using System.Data;
using com.SEO.Business;
using com.SEO.Model;
using ExtAspNet;
using Helper.Bussiness;

namespace SEO_Server.Pages
{
    public partial class UserManagePage : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                wndAddUser.Title = "添加帳戶";
                btnAddUser.OnClientClick = wndAddUser.GetShowReference("AddUser.aspx");
                wndAddUser.OnClientCloseButtonClick = wndAddUser.GetHidePostBackReference();

                wndEditURL.OnClientCloseButtonClick = wndEditURL.GetHidePostBackReference();

                BindGrid();
            }
        }

        #region Private Method

        /// <summary>
        /// Bind Grid
        /// </summary>
        private void BindGrid()
        {
            CommSelect _commSelect = new CommSelect();
            ComHelp _comHelp = new ComHelp();
            _comHelp.TableName = "Users";
            _comHelp.SelectList = "*";
            _comHelp.SearchCondition = " UserType = 1";
            _comHelp.PageSize = PageCounts;
            _comHelp.PageIndex = gridUsers.PageIndex;
            _comHelp.OrderExpression = "UserAccount asc";

            DataTable dtbUsers = _commSelect.ComSelect(ref _comHelp);
            gridUsers.RecordCount = _comHelp.TotalCount;
            gridUsers.PageSize = PageCounts;

            gridUsers.DataSource = dtbUsers.Rows;
            gridUsers.DataBind();

            gridUsers.SelectedRowIndexArray = new int[0];
        }

        #endregion

        #region Page Event

        /// <summary>
        /// Data Bound
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridUsers_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            if (e.DataItem != null)
            {
                SiteManage siteManage = new SiteManage();
                if (!e.Values[1].ToString().Equals(Guid.Empty.ToString()))
                {
                    SiteInfo siteInfo = siteManage.GetSiteInfoByID(e.Values[1].ToString());
                    if (siteInfo != null)
                    {
                        e.Values[3] = siteInfo.SiteURL;
                    }
                }
            }
        }

        /// <summary>
        /// Page Index Change
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridUsers_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            gridUsers.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        /// <summary>
        /// Operation Click Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gridUsers_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            string strUserID = ((GridRow)gridUsers.Rows[e.RowIndex]).Values[0];

            if (e.CommandName == "Edit")
            {
                wndEditURL.IFrameUrl = "EditSitePage.aspx?UserIDs=" + strUserID;
                wndEditURL.Hidden = false;
            }
            else if (e.CommandName == "Delete")
            {
                UserManage userManage = new UserManage();
                UserInfo userInfo = userManage.GetUserInfoByID(strUserID);
                if (userInfo != null)
                {
                    userManage.DeleteUserInfo(userInfo);

                    BindGrid();
                }
            }
        }

        /// <summary>
        /// Add User
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void wndAddUser_Close(object sender, ExtAspNet.WindowCloseEventArgs e)
        {
            BindGrid();
        }

        /// <summary>
        /// Delete Selected User Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDeleteSelectedUsers_Click(object sender, EventArgs e)
        {
            UserManage userManage = new UserManage();

            if (gridUsers.SelectedRowIndexArray.Count() == 0)
            {

                Alert.Show("請選擇帳戶!");
                return;
            }

            foreach (int index in gridUsers.SelectedRowIndexArray)
            {
                string strUserID = ((GridRow)gridUsers.Rows[index]).Values[0];
                UserInfo userInfo = userManage.GetUserInfoByID(strUserID);
                if (userInfo != null)
                {
                    userManage.DeleteUserInfo(userInfo);
                }
            }

            BindGrid();
        }

        /// <summary>
        /// Modify Selected Sites
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnModifySelectedSites_Click(object sender, EventArgs e)
        {
            if (gridUsers.SelectedRowIndexArray.Count() == 0)
            {

                Alert.Show("請選擇帳戶!");
                return;
            }

            string strUserIDs = null;
            foreach (int index in gridUsers.SelectedRowIndexArray)
            {
                if (index == 0)
                {
                    strUserIDs += ((GridRow)gridUsers.Rows[index]).Values[0];
                }
                else
                {
                    strUserIDs += "|" + ((GridRow)gridUsers.Rows[index]).Values[0];
                }
            }

            wndEditURL.IFrameUrl = "EditSitePage.aspx?UserIDs=" + strUserIDs;
            wndEditURL.Hidden = false;
        }

        /// <summary>
        /// Edit URL Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void wndEditURL_Close(object sender, WindowCloseEventArgs e)
        {
            BindGrid();
        }

        #endregion
    }
}